package com.threeonemeeting.activity;

import java.util.Map;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.snda.lib.http.TaskManager;
import com.snda.lib.ui.MessageBox;
import com.threeonemeeting.Const;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.R;
import com.threeonemeeting.activity.ext.MeetingPageListActivity;
import com.threeonemeeting.db.AppDBHelper;
import com.threeonemeeting.list.FullMeetingInfoList;
import com.threeonemeeting.listview.DragRefreshListView;
import com.threeonemeeting.model.FullMeetingInfo;
import com.threeonemeeting.model.MeetingInfo;
import com.threeonemeeting.tab.MultiActivityTab;
import com.threeonemeeting.task.HttpConst;
import com.threeonemeeting.task.LoadRecentMeetingTask;
import com.threeonemeeting.util.MiscUtil;

public class RecentMeetingListActivity extends MeetingPageListActivity implements
		ICallBack {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_rencent_meeting_list, "最近浏览");

		Button btnClear = (Button) findViewById(R.id.btn_clear);
		btnClear.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				DialogInterface.OnClickListener confirmListener = new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						AppDBHelper db = DataCenter.getInstance().getDB();
						db.clearMeetingVisitTime();

						loadRecentMeeting(1);
					}

				};

				MultiActivityTab tabWnd = (MultiActivityTab) RecentMeetingListActivity.this
						.getParent();
				MessageBox box = new MessageBox(tabWnd);
				box.showDialog("提示", "确定要清除所有的浏览记录?", confirmListener);
			}
		});

		// ListView
		meetingListView
				.setOnRefreshListener(new DragRefreshListView.OnRefreshListener() {
					public void onRefresh() {
						meetingListView.reset();

						LoadRecentMeetingTask task = new LoadRecentMeetingTask(
								RecentMeetingListActivity.this,
								RecentMeetingListActivity.this);
						task.setPages(1);
						TaskManager.addTask(task, null, false);
					}
				});

		meetingListView
				.setOnLoadMoreListener(new DragRefreshListView.OnLoadMoreListener() {

					public void onLoadMore(int nNextPage) {
						loadRecentMeeting(nNextPage);

						showLoadingMoreLayout(true);
					}
				});

		// 从DB中加载
		loadRecentMeeting(1);
	}

	private void loadRecentMeeting(int nPage) {
		LoadRecentMeetingTask task = new LoadRecentMeetingTask(
				RecentMeetingListActivity.this, RecentMeetingListActivity.this);
		task.setPages(nPage);
		TaskManager.addTask(task, null, false);
	}

	public void doCallBack(Map<String, Object> map) {
		Integer msgType = (Integer) map.get(HttpConst.KEY_TASK_TYPE);
		if (msgType == null) {
			return;
		}

		Integer nError = (Integer) map.get(HttpUtil.KEY_ERROR_CODE);
		if (nError == null || nError != HttpUtil.ERROR_SUCCESS) {
			return;
		}

		if (msgType != HttpConst.TASKTYPE_LOADRECENTMEETING)
			return;

		meetingListView.stopLoadingMore();

		FullMeetingInfoList listMeeting = (FullMeetingInfoList) map.get("list");
		if (listMeeting == null)
			return;

		Integer nPage = (Integer) map.get("page");
		if (nPage == null)
			nPage = 1;

		if (nPage == 1) {
			mAdapter.clear();
			MiscUtil.showListLayout(this, listMeeting.size());
		}

		if (listMeeting.size() == 0 || listMeeting.size() < Const.PAGE_NUM) {
			meetingListView.setLastPage(nPage);
		}

		for (int i = 0; i < listMeeting.size(); i++) {
			FullMeetingInfo fullInfo = listMeeting.get(i);
			if (fullInfo == null)
				continue;

			DataCenter.getInstance().listFullMeeting
					.addFullMeetingInfo(fullInfo);

			MeetingInfo info = new MeetingInfo(fullInfo);
			mAdapter.add(info);
		}

		loadingLayout.setVisibility(View.GONE);
		listLayout.setVisibility(View.VISIBLE);

		mAdapter.notifyDataSetChanged();

		meetingListView.onRefreshComplete();
		meetingListView.onLoadMoreComplete();

		showLoadingMoreLayout(false);
	}
}
